<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="guestbookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="留言内容" align="center" prop="content" />
      <el-table-column label="留言时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="sendMsgView(scope.row)"
            v-hasPermi="['system:guestbook:edit']"
          >回复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改留言板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <div class="chat-card">
      <h2 class="text-center mb-4">透策留言板({{userName}})</h2>
      <div class="msg-box">
      <div class="msg-list" ref="msgListEl">
      <div
      class="msg-item"
      :class="item.type == 1 ? 'left-msg' : 'right-msg'"
      v-for="item in msgList"
      :key="item"
      >
      <div class="logo" v-if="item.type == 1">用户</div>
      <div class="msg-content">{{ item.content }}</div>
      <div class="logo" v-if="item.type == 2">我</div>
      </div>
      </div>
      </div>
      <div class="reply-box">
      <el-input
      v-model="replyMsg"
      :autosize="{ minRows: 1, maxRows: 4 }"
      maxlength="666"
      placeholder="说点什么吧......."
      type="textarea"
      />
      <div
      class="send-btn"
      :class="replyMsg ? 'active-btn' : ''"
      >
        <i class="el-icon-position" @click="addGuestbook"></i>

      </div>
      </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGuestbook,
  addGuestbook,
  updateGuestbook,
  getInfo
} from "@/api/system/guestbook";

export default {
  name: "Guestbook",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留言板表格数据
      guestbookList: [],
      replyMsg:'',
      msgList:[],
      userName:'',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        userId:null,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询留言板列表 */
    getList() {
      this.loading = true;
      listGuestbook(this.queryParams).then(response => {
        this.guestbookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        content: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    toBottom(){
      this.$refs["msgListEl"].scrollTop = this.$refs["msgListEl"].scrollHeight;
    },
    sendMsgView(row) {
      this.open=true;
      this.queryParams.userId = row.userId;
      this.userName = row.userName;
      getInfo(this.queryParams).then(response => {
        this.msgList = response.data;
      });
      setTimeout(this.toBottom
      ,300);

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
  async addGuestbook() {
      this.form.userId = this.queryParams.userId;
      this.form.content = this.replyMsg;
      this.form.type = 2;
      await addGuestbook(this.form).then(response => {
          this.reset();
        this.replyMsg = '';
        });
    await getInfo(this.queryParams).then(response => {
        this.msgList = response.data;
      });

      this.$refs["msgListEl"].scrollTop = this.$refs["msgListEl"].scrollHeight;
    },
  }
};
</script>


<style scoped lang="scss">
.chat-card {
  padding: 1.5rem; /* Equivalent to p-6 in Tailwind */

  /* Message Box */
  .msg-box {
    background-color: #f1f1f1; /* Equivalent to bg-gray-100 */
    margin-bottom: 1.25rem; /* Equivalent to mb-5 */
    border-radius: 0.375rem; /* Equivalent to rounded-md */
    padding: 0.5rem; /* Equivalent to p-2 */

    /* Message List */
    .msg-list {
      flex-wrap: wrap;
      align-items: baseline;
      overflow-y: auto;
      max-height: 500px;
      min-height: 500px;

      &::-webkit-scrollbar {
        width: 5px;
      }

      /* Message Item */
      .msg-item {
        width: 100%;
        margin-top: 0.5rem; /* Equivalent to my-2 */
        display: flex;

        /* Message Content */
        .msg-content {
          color: #fff; /* Equivalent to text-white */
          padding: 0.75rem; /* Equivalent to px-3 py-2 */
          border-radius: 0.375rem; /* Equivalent to rounded-md */
          max-width: 60%;
        }

        /* Logo */
        .logo {
          width: 2rem; /* Equivalent to w-8 */
          height: 2rem; /* Equivalent to h-8 */
          border-radius: 0.375rem; /* Equivalent to rounded-md */
          display: flex;
          justify-content: center;
          align-items: center;
          color: #fff; /* Equivalent to text-white */
        }
      }

      /* Left Message */
      .left-msg {
        justify-content: flex-start;

        /* Logo */
        .logo {
          background-color: #3498db; /* Equivalent to bg-theme-color */
          margin-right: 0.5rem; /* Equivalent to mr-2 */
        }

        /* Message Content */
        .msg-content {
          background-color: #fff; /* Equivalent to bg-white */
          color: #000; /* Equivalent to text-black */
        }
      }

      /* Right Message */
      .right-msg {
        justify-content: flex-end;

        /* Logo */
        .logo {
          background-color: #f39c12; /* Equivalent to bg-yellow-400 */
          margin-left: 0.5rem; /* Equivalent to ml-2 */
        }

        /* Message Content */
        .msg-content {
          background-color: #4caf50; /* Equivalent to bg-green-400 */
          color: #fff; /* Equivalent to text-white */
        }
      }
    }
  }

  /* Reply Box */
  .reply-box {
    margin: 0 auto;
    position: relative;
    width: 70%;

    /* Textarea Inner */
    ::v-deep .el-textarea__inner{
      min-height: 41px !important;
      height: 41px;
      padding: 10px 30px 10px 10px;
    }

    /* Send Button */
    .send-btn {
      background-color: transparent;
      color: #ccc; /* Equivalent to text-gray-200 */
      font-size: 1.5rem; /* Equivalent to text-xl */
      position: absolute;
      right: 1.5rem; /* Equivalent to right-3 */
      border-radius: 0.375rem; /* Equivalent to rounded-md */
      padding: 0.25rem; /* Equivalent to p-1 */
      display: flex;
      justify-content: center;
      align-items: center;
      bottom: 6px;
    }

    /* Active Button */
    .active-btn {
      background-color: #3498db; /* Equivalent to bg-theme-color */
      color: #fff; /* Equivalent to text-white */
    }
  }
}


</style>
